home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  2.7 KB  |  115 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <memory.h>
  7. #include "lib.h"
  8.  
  9. #define _NFILE NFILES
  10.  
  11. FILE    _iob[_NFILE] =            /* stream buffers */
  12. {
  13.    /*  stdin */    {0, NULL, NULL, (_IOREAD | _IOFBF), 0, 0, '\0'},
  14.    /* stdout */    {0, NULL, NULL, (_IOWRT | _IOLBF), 1, 0, '\0'},
  15.    /* stderr */    {0, NULL, NULL, (_IOWRT | _IONBF), 2, 0, '\0'},
  16.         {0, NULL, NULL, 0, 0, 0, '\0'},
  17.         {0, NULL, NULL, 0, 0, 0, '\0'},
  18.         {0, NULL, NULL, 0, 0, 0, '\0'},
  19.         {0, NULL, NULL, 0, 0, 0, '\0'},
  20.         {0, NULL, NULL, 0, 0, 0, '\0'},
  21.         {0, NULL, NULL, 0, 0, 0, '\0'},
  22.         {0, NULL, NULL, 0, 0, 0, '\0'},
  23.         {0, NULL, NULL, 0, 0, 0, '\0'},
  24.         {0, NULL, NULL, 0, 0, 0, '\0'},
  25.         {0, NULL, NULL, 0, 0, 0, '\0'},
  26.         {0, NULL, NULL, 0, 0, 0, '\0'},
  27.         {0, NULL, NULL, 0, 0, 0, '\0'},
  28.         {0, NULL, NULL, 0, 0, 0, '\0'},
  29.         {0, NULL, NULL, 0, 0, 0, '\0'},
  30.         {0, NULL, NULL, 0, 0, 0, '\0'},
  31.         {0, NULL, NULL, 0, 0, 0, '\0'}
  32.         };
  33.  
  34. size_t __DEFAULT_BUFSIZ__;
  35.  
  36. void _main(_argc, _argv, _envp)
  37. long _argc;
  38. char **_argv, **_envp;
  39. {
  40.     void exit(int);
  41.     register FILE *f;
  42.     void _getbuf(FILE *);
  43.     int main(int, char **, char **);
  44.     
  45.     if(!__DEFAULT_BUFSIZ__) __DEFAULT_BUFSIZ__ = BUFSIZ;
  46.     
  47.     for(f = &_iob[0]; f < &_iob[3]; f++)    /* flag device streams */
  48.     {
  49.     if(isatty(f->_file))
  50.         f->_flag |= _IODEV;
  51.     else
  52.         if(f == stdout)
  53.         { /* stdout re-directed, make it full buffered */
  54.         f->_flag &= ~_IOLBF;
  55.         f->_flag |=  _IOFBF;
  56.         }
  57.     _getbuf(f);    /* get a buffer */
  58.     }
  59.     
  60.     (void)main((int) _argc, _argv, _envp);    /* if main() returns... */
  61.     exit(EXIT_SUCCESS);            /* ...exit with OK status */
  62. }
  63.  
  64. void _exit(status)
  65. int status;
  66. {
  67.     (void) callm1(MM, EXIT, status, 0, 0, NIL_PTR, NIL_PTR, NIL_PTR);
  68. }
  69.  
  70. /* functions registered by user for calling at exit */
  71. #ifdef __STDC__
  72. typedef void (*ExitFn)(void);
  73. #else
  74. typedef void (*ExitFn)();
  75. #endif
  76. static ExitFn *_at_exit;
  77. static int num_at_exit = 0;    /* number of functions registered - 1 */
  78.  
  79. void exit(status)
  80. int status;
  81. {
  82.     register int i, f;
  83.     
  84.     for(i = num_at_exit - 1; i >= 0; --i)
  85.     {
  86.     (*_at_exit[i])();
  87.     }
  88.     for(i=0; i<_NFILE; ++i)
  89.     {
  90.     f = _iob[i]._flag;
  91.     if(f & (_IORW | _IOREAD | _IOWRT))
  92.         fclose(&_iob[i]);
  93.     }
  94.     _exit(status);
  95. }
  96.  
  97. /* register a function for execution on termination */
  98. /* Ansi requires atleast 32 entries, we make it dynamic and hope
  99.    it meets the ansi requirement */
  100.  
  101. int atexit(func)
  102. ExitFn func;
  103. {
  104.     if (num_at_exit == 0)
  105.     _at_exit = (ExitFn *)malloc((size_t)sizeof(ExitFn));
  106.     else
  107.     _at_exit = (ExitFn *)realloc(_at_exit, (size_t)((num_at_exit + 1) *
  108.                          sizeof(ExitFn))); 
  109.     if(_at_exit == (ExitFn *)NULL)
  110.     return -1;    /* failure */
  111.     
  112.     _at_exit[num_at_exit++] = func;
  113.     return 0;        /* success */
  114. }
  115.